import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:convert/convert.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

String getAssetPath(String name, [String ext = 'png']) {
  return 'assets/images/$name.$ext';
}

class Util {
  // md5 加密
  static String generateMd5(String data) {
    var content = new Utf8Encoder().convert(data);
    var digest = md5.convert(content);
    // 这里其实就是 digest.toString()
    return hex.encode(digest.bytes);
  }

  /// 隐藏键盘
  static hiddenKeyBorder(context) =>
      FocusScope.of(context).requestFocus(FocusNode());

  /// toask
  static showMsg(String title) {
    Fluttertoast.showToast(
        textColor: Colors.white,
        backgroundColor: Colors.black54,
        msg: title,
        gravity: ToastGravity.CENTER,
        toastLength: Toast.LENGTH_LONG);
  }

  static showBottomMsg(String title) {
    Fluttertoast.showToast(
        textColor: Colors.white,
        backgroundColor: Colors.black54,
        msg: title,
        gravity: ToastGravity.BOTTOM,
        toastLength: Toast.LENGTH_SHORT);
  }

  static showTopMsg(String title) {
    Fluttertoast.showToast(
        textColor: Colors.white,
        backgroundColor: Colors.black54,
        msg: title,
        gravity: ToastGravity.TOP,
        toastLength: Toast.LENGTH_SHORT);
  }

  /// 电话号码：1开头，后面10位数字
  static bool isPhone(String input) {
    RegExp mobile = new RegExp(r"1[0-9]\d{9}$");
    return mobile.hasMatch(input);
  }

  /// 登录密码：6~16位数字和字符组合
  static bool isLoginPassword(String input) {
    RegExp mobile = new RegExp(r"(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$");
    return mobile.hasMatch(input);
  }

  /// 支付密码：6位数字验证码
  static bool isValidateCaptcha(String input) {
    RegExp mobile = new RegExp(r"\d{6}$");
    return mobile.hasMatch(input);
  }
}
